﻿<?php
class sscls_CustommerController extends sscls_Controller
{
    function __construct()
    {
        $this->view='article';
    }
	
	function view()
	{
		$model=$this->getModel();
		$values['cus']=$model->getCus();
		$this->layout='list';
		$this->view='custommer';
		$this->display($values);
		return;
	}
    function add()
	{	
		
		$model=$this->getModel();
		$id=sscls_Request::getRequest('id');
		$vals['item']=$model->getItem($id);
		$this->layout='add';
		$this->view='custommer';
		$this->display($vals);
		
	}
	function remove()
	{
		$cid=$_POST['ids'];
		$model=$this->getModel();
		$model->remove($cid);
		$this->setRedirec('index.php?c=custommer');
		return;
	}
	
	function sendmailtmpl()
	{
		$model=$this->getModel();
		$articles=$model->getArticles();
		$session=ss_Main::getSession();
		if(isset($_POST['ids']))
		{
			$vals['customer']=$model->getCustomer($_POST['ids']);
			$session->set('customer',$vals['customer']);
		}
		else
		if(!is_null($session->get('customer')))
		{
			$vals['customer']=$session->get('customer');
		}
		$id=sscls_Request::getRequest('articleid');
		if((int)$id)
		{
			$vals['item']=$model->getArticle($id);
		}
		
		$vals['articles']=$articles;
		$this->layout='sendmail';
		$this->view='custommer';
		$this->display($vals);
	}
	function proccesssendmail()
	{
		$post=sscls_Request::get('post');
		$from='namthanh.info@yahoo.com';
		$subject='Mail từ Nam Thành';
		$session=ss_Main::getSession();
		if(!is_null($session->get('customer')))
		{
			$customer=$session->get('customer');
			foreach((array)$customer as  $cus)
			{
				if(!$this->sendmail($cus->email,$from,$subject,$post['content']))
				{
					$this->setRedirec('index.php?c=custommer','Không thể gửi mail. Vui lòng kiểm tra cấu hình email trên server của bạn.','error');
					return false;
					break;
					
				}
			}
			$this->setRedirec('index.php?c=custommer','Gửi thành công');
			return true;

		}else
		{
			$this->setRedirec('index.php?c=custommer','Vui lòng chọn danh sách gửi mail.','error');
			return false;
		}
		
		
	}
	function proccessadd()
	{
		$post=sscls_Request::get('post');

		$model=$this->getModel();
		$post['date_buy']=strtotime($post['date_buy']);
		$post['date_end']=strtotime($post['date_end']);
		if($model->store1($post,'custommer'))
		{
			$this->setRedirec('index.php?c=custommer','Cập nhật thành công');
			return;
		}
		$this->setRedirec('index.php?c=custommer&t=add&id='.$post['id'],'Đã có lỗi trong quá trình lưu. Vui lòng thử lại','error');
		return;
	}
	
	function upload($url,$urlback)
	{	
		$user=ss_Main::getUser();
		
		//define a maxim size for the uploaded images in Kb
		 define ("MAX_SIZE","10000"); 

		//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
		//and it will be changed to 1 if an errro occures.  
		//If the error occures the file will not be uploaded.
		 $errors=0;
		//checks if the form has been submitted
		 if(isset($_POST['Submit'])) 
		 {
			//reads the name of the file the user submitted for uploading
			$image=$_FILES['image']['name'];
			//if it is not empty
			if ($image) 
			{
			//get the original name of the file from the clients machine
				$filename = stripslashes($_FILES['image']['name']);
			//get the extension of the file in a lower case format
				$extension = $this->getExtension($filename);
				$extension = strtolower($extension);
			//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
			//otherwise we will do more tests
		 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
				{
				//print error message
					$this->setRedirec($urlback,'Dịnh dạng file không hợp lệ','error');
					return null;
					$errors=1;
				}
				else
				{
		//get the size of the image in bytes
		 //$_FILES['image']['tmp_name'] is the temporary filename of the file
		 //in which the uploaded file was stored on the server
		 $size=filesize($_FILES['image']['tmp_name']);

		//compare the size with the maxim size we defined and print error if bigger
		if ($size > MAX_SIZE*1024)
		{
			$this->setRedirec($urlback,'Vui lòng chọn file kích thước nhỏ hơn','error');
			return null;
			$errors=1;
		}

		//we will give an unique name, for example the time in unix time format
		$image_name=md5($user->id).time().'.'.$extension;
		//the new name will be containing the full path where will be stored (images folder)
		$newname=$url.$image_name;
		//we verify if the image has been uploaded, and print error instead
		$copied = copy($_FILES['image']['tmp_name'], $newname);
		if (!$copied) 
		{
			$this->setRedirec($urlback,'Không thể coppy hình ảnh lên server. Bạn vui lòng kiểm tra phân quyền của thư mục cần đưa lên','error');
			return null;
			$errors=1;
		}}}}

		//If no errors registred, print the success message
		 
		return $image_name;
	}
	
	
		//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
		 function getExtension($str) {
				 $i = strrpos($str,".");
				 if (!$i) { return ""; }
				 $l = strlen($str) - $i;
				 $ext = substr($str,$i+1,$l);
				 return $ext;
		 }
		function sendmail($to,$from,$subject, $message)
		{
			//end of message
			$headers  = "From: $from\r\n";
			$headers .= "Content-type: text/html\r\n";

			//options to send to cc+bcc
			//$headers .= "Cc: [email]maa@p-i-s.cXom[/email]";
			//$headers .= "Bcc: [email]email@maaking.cXom[/email]";

			// now lets send the email.
			if(@mail($to, $subject, $message, $headers))
			{return true;}
			else
			return false;
		}
    
}
 
?>